package com.sduept.nwld.dataserver.controller.document;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.document.HtmlDocumentManager;
import com.sduept.nwld.dataserver.model.HtmlDocument;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
@Named
@ViewScoped
public class HtmlDocumentController extends AbstractController implements
Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3234604423487665000L;
	
	@Autowired
	private HtmlDocumentManager hdm;
	
	private String text=null;
	private List<HtmlDocument> htmlDocs=null;
	private HtmlDocument selected=null;
	private HtmlDocument create;
	private TreeNode selectedNode;
	private TreeNode root;
	
	private String selectedTitle=null;
	@PostConstruct
	public void init() {
		initTree();
	}
	public void initTree(){
		htmlDocs=hdm.findAllHtmlDocuments();
		root = new DefaultTreeNode();
		for(HtmlDocument hd:htmlDocs){
			TreeNode newNode=new LibTreeNode(HtmlDocument.class.getSimpleName(), hd, root);
			if(selectedTitle!=null&&hd.getTitle().equals(selectedTitle)){
				newNode.setSelected(true);
				selected=(HtmlDocument) newNode.getData();
			}
		}
	}
	public void onNodeSelect(NodeSelectEvent event){
		selected = (HtmlDocument) selectedNode.getData();
		if(selected!=null){
			selectedTitle=selected.getTitle();
			text=selected.getDescription();
		}
	}
	/**
	 * 编辑器保存内容
	 * */
	public void saveText(){
		if(StringUtils.isNotEmpty(text)){
			selected.setDescription(text);
			try {
				hdm.createOrUpdateHtmlDocument(selected);
				addSuccessMessage("保存成功");
			} catch (Exception e) {
				e.printStackTrace();
				addErrorMessage("保存失败");
			}
		}else{
			addErrorMessage("内容为空！");
		}
	}
	public void preCreate(){
		create=new HtmlDocument();
	}
	/**
	 * 新建标题
	 * */
	public void createTitle(){
		try {
			for(HtmlDocument hd:htmlDocs){
				if(hd.getTitle().equals(create.getTitle())){
					addErrorMessage("该标题已存在！");
					return;
				}
			}
			selectedTitle=create.getTitle();
			hdm.createOrUpdateHtmlDocument(create);
			initTree();
			text="";
			addSuccessMessage("新建成功");
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("新建失败");
		}
	}
	/**
	 * 编辑标题
	 * */
	public void updateTitle(){
		try {
			if(selectedTitle.equals(selected.getTitle())) {
				return;
			}
			for(HtmlDocument hd:htmlDocs){
				if(hd.getTitle().equals(selected.getTitle())&&!hd.getId().equals(selected.getId())){
					addErrorMessage("该标题已存在！");
					selected.setTitle(selectedTitle);
					return;
				}
			}
			hdm.createOrUpdateHtmlDocument(selected);
			addSuccessMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("保存失败");
		}
	}
	/**
	 * 删除标题
	 * */
	public void deleteTitle(){
		if(selected!=null){
			try {
				hdm.delHtmlDocument(selected);
				addSuccessMessage("删除成功");
				initTree();
				selected=null;
				text="";
				selectedTitle=null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			addErrorMessage("请选择标题");
			return;
		}
	}
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public List<HtmlDocument> getHtmlDocs() {
		return htmlDocs;
	}
	public void setHtmlDocs(List<HtmlDocument> htmlDocs) {
		this.htmlDocs = htmlDocs;
	}
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public TreeNode getSelectedNode() {
		return selectedNode;
	}
	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}
	public HtmlDocument getSelected() {
		return selected;
	}
	public void setSelected(HtmlDocument selected) {
		this.selected = selected;
	}
	public HtmlDocument getCreate() {
		return create;
	}
	public void setCreate(HtmlDocument create) {
		this.create = create;
	}
	

}
